package com.compass.mobile.ui.activity

import AppFrontBackListener
import CompassAppHelper
import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.multidex.MultiDex
import com.compass.mobile.MobileApplication
import com.compass.mobile.R
import com.compass.mobile.databinding.ActivityMainBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.constant.NIM_APP_KEY
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.ui.fragment.HospitalChoiceFragment
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.SDKOptions
import com.netease.nimlib.sdk.auth.LoginInfo
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVLogLevel
import getStringFromResource


/**
 * Description： 首页选择医院
 * Date:2024/5/20 16:20
 */
class MainActivity : BaseVBActivity<ActivityMainBinding>() {


    override fun initView(savedInstanceState: Bundle?) {

        displaySelectedFragment(HospitalChoiceFragment())
    }



    override fun initData() {
        AppManager.init(CompassAppHelper.getApplication())
        MultiDex.install(CompassAppHelper.getApplication())
        initMVVK()
        TipsToast.init(CompassAppHelper.getApplication())
        initNIMClient()

    }


    private fun initMVVK() {
        MMKV.initialize(this)
        MMKV.setLogLevel(
            if (CompassAppHelper.isDebug()) {
                MMKVLogLevel.LevelDebug
            } else {
                MMKVLogLevel.LevelError
            }
        )
    }


    /**
     * 初始化IM_SDK
     */
    private fun initNIMClient() {
        val options = getSDKOptions(NIM_APP_KEY)
        NIMClient.init(this, null, options)

    }

    private fun getSDKOptions(appKey: String?): SDKOptions {
        val options = SDKOptions()
        options.appKey = appKey
        options.preloadAttach = true
        options.disableAwake = true
        options.sessionReadAck = true
        options.animatedImageThumbnailEnabled = true
        options.asyncInitSDK = true
        options.reducedIM = false
        //是否在 SDK 初始化时检查 Manifest 文件配置是否完成，默认为 false，不检查。
        //par使用建议： 建议在调试阶段开启，上线时关闭
        options.checkManifestConfig = true
        options.enableTeamMsgAck = true
        return options
    }

    /**
     * 注册APP前后台切换监听
     */
    private fun appFrontBackRegister(application: Application) {
        AppFrontBack.register(application, object : AppFrontBackListener {
            override fun onBack(activity: Activity?) {
                LogUtil.d("onBack")
            }

            override fun onFront(activity: Activity?) {
                LogUtil.d("onFront")
            }
        })
    }

    // 根据需要显示或替换Fragment
    private fun displaySelectedFragment(fragment: Fragment) {
        val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentContainer, fragment)
        fragmentTransaction.commit()
    }


    private var mLastClickBackTime: Long = 0 //上次点击back键的时间

    override fun onBackPressed() {
        val current = System.currentTimeMillis()
        if (current - mLastClickBackTime > 2000) {
            mLastClickBackTime = current
            showToast(getStringFromResource(R.string.base_app_exit_one_more_press))
        } else {
            super.onBackPressed()
        }
    }


}